const userApi = require('../../api/user.js')
Page({
  data: {
    userId: '',
    userInfo: {},
    bottomPadding:''
  },
  async onShow() {
    let jump = getApp().globalData.jump
    if (jump) {
      getApp().globalData.jump = false;
      wx.showToast({
        title: '请先填写用户信息后再操作',
        icon: 'none',
        duration: 2000
      })
    }
    this.setData({ bottomPadding: getApp().globalData.bottomPadding })
    if (typeof this.getTabBar === 'function' && this.getTabBar()) {
      this.getTabBar().setData({
        selected: 2
      })
    }
    wx.setNavigationBarTitle({
      title: '我的'
    })
    //修改头像、昵称、电话存到服务器上返回我的页面时，从服务器重新拿loginInfo数据并存到storage中
    this.userId = wx.getStorageSync('loginInfo').userId
    let res = await userApi.getUserInfo({
      userId: this.userId
    })
    this.setData({
      userInfo: res.data
    })
    wx.setStorageSync('userInfo', res.data)
  },
  publishClick() {
    if (this.data.userInfo && this.data.userInfo.avatarUrl && this.data.userInfo.nickName && this.data.userInfo.phoneNumber) {
      wx.navigateTo({
        url: '/pages/user/publish/index'
      })
    } else {
      wx.showToast({
        title: '请先填写用户信息后再操作',
        icon: 'none',
        duration: 2000
      })
    }
  },
  clickEditNameCard() {
    if (this.data.userInfo && this.data.userInfo.avatarUrl && this.data.userInfo.nickName && this.data.userInfo.phoneNumber) {
      wx.navigateTo({
        url: '/pages/user/editNameCard/index'
      })
    } else {
      wx.showToast({
        title: '请先填写用户信息后再操作',
        icon: 'none',
        duration: 2000
      })
    }
  },
  clickInterCard(e) {
    const type = e.currentTarget.dataset['type']
    wx.navigateTo({
      url: `/pages/user/listPage/index?type=${type}`
    })
  },
  clickEditCard(e) {
    const type = e.currentTarget.dataset['type']
    wx.navigateTo({
      url: `/pages/user/editInfoPage/index?type=${type}`
    })
  },
  messageClick(e) {
    let userId = wx.getStorageSync('loginInfo').userId
    wx.navigateTo({
      url: `/pages/home/message/index?id=${userId}`
    })
  },
  aboutClick(e) {
    // wx.navigateTo({
    //   url: `/pages/home/about/index`
    // })
  },
  clickEditBusinessCard(e) {
    if (this.data.userInfo && this.data.userInfo.avatarUrl && this.data.userInfo.nickName && this.data.userInfo.phoneNumber) {
      wx.navigateTo({
        url: '/pages/user/editBusinessCard/index'
      })
    } else {
      wx.showToast({
        title: '请先填写用户信息后再操作',
        icon: 'none',
        duration: 2000
      })
    }
  }
})